<?php

namespace  app\service;


use app\model\CarModel;
use app\model\DriverModel;
use app\model\NetworkBussinessModel;
use app\model\PartnerLogModel;
use app\model\PartnerUserModel;
use app\model\WarehouseModel;
use think\facade\Db;

Class NetworkBussinessService{

    public static  function listsInfo($param)
    {
        return NetworkBussinessModel::listsInfo($param);
    }
    public static  function info()
    {

        $warehouseType=WarehouseModel::GetAllWarehouseType();
        $bussineType=WarehouseModel::GetAllBussineType();
        return [
            'warehouseType'=>$warehouseType,
            'bussineType'=>$bussineType
        ];
    }

    public  static  function  add($data,$logParam,$time_requirementsRes)
    {


        Db::startTrans();
       $data['add_time']=getNowDateTime();

       $resId= NetworkBussinessModel::addId($data);
       foreach ($time_requirementsRes as $k=>$v){
           $addRes=NetworkBussinessModel::addTime(['status'=>1,'add_time'=>getNowDateTime(),'start_time'=>$v['start_time'],
               'end_time'=>$v['end_time'],'partner_id'=>$data['partner_id'],'business_id'=>$resId]);
           if (!$addRes){
               // 回滚事务
               Db::rollback();
               return 10005;
           }
       }
        $dataLog=[
            'partner_user_id'=>$logParam['uid'],
            'log_type'=>2,
            'request_ip'=>$logParam['ip'],
            'add_time'=>getNowDateTime(),
            'log_desc'=>'添加网点业务',
            'status'=>1,
            'partner_id'=>$data['partner_id']
        ];
        
        if ($resId){
            Db::commit();
            return 10000;
        }else{
            // 回滚事务
            Db::rollback();
            return 10003;
        }
    }
    public  static  function  edit($data,$logParam,$id,$time_requirementsRes)
    {


        Db::startTrans();
        $data['update_time']=getNowDateTime();
        $res= NetworkBussinessModel::edit($data,$id);
        $dataLog=[
            'partner_user_id'=>$logParam['uid'],
            'log_type'=>2,
            'request_ip'=>$logParam['ip'],
            'add_time'=>getNowDateTime(),
            'log_desc'=>'修改网点业务时间信息',
            'status'=>1,
            'partner_id'=>$data['partner_id']
        ];
        $editStatusRes=NetworkBussinessModel::editTimeStatus($id);
        if (!$editStatusRes){
            // 回滚事务
            Db::rollback();
            return 10006;
        }
        $newtime_requirementsRes=json_decode($time_requirementsRes,true);

        foreach ($newtime_requirementsRes as $k=>$v){
            //存在，修改
            if (count($v)>2){
                $editRes=NetworkBussinessModel::editTime(['status'=>1,'add_time'=>time(),'start_time'=>$v['start_time'],
                    'end_time'=>$v['end_time'],'partner_id'=>$data['partner_id']],$v['id']);
                if (!$editRes){
                    // 回滚事务
                    Db::rollback();
                    return 10005;
                }
            }
            //不存在，新增
            else{
                $addRes=NetworkBussinessModel::addTime(['status'=>1,'add_time'=>time(),'start_time'=>$v['start_time'],
                    'end_time'=>$v['end_time'],'partner_id'=>$data['partner_id'],'business_id'=>$id]);
                if (!$addRes){
                    // 回滚事务
                    Db::rollback();
                    return 10005;
                }
            }

            //删除

        }
        
        if ($res){
            Db::commit();
            return 10000;
        }else{
            // 回滚事务
            Db::rollback();
            return 10003;
        }
    }
    public  static  function  del($data,$logParam,$id)
    {


        Db::startTrans();
        $res= NetworkBussinessModel::del($id);
        $dataLog=[
            'partner_user_id'=>$logParam['uid'],
            'log_type'=>2,
            'request_ip'=>$logParam['ip'],
            'add_time'=>getNowDateTime(),
            'log_desc'=>'删除网点业务信息',
            'status'=>1,
            'partner_id'=>$data['partner_id']
        ];
        
        if ($res){
            Db::commit();
            return 10000;
        }else{
            // 回滚事务
            Db::rollback();
            return 10003;
        }
    }
}